<template>
  <div>
    <div class="content">
      <div class="return" @click="close">
        <img class="img1" src="../../assets/img/return.png" alt="" />
        <p class="return-p">返回</p>
      </div>
      <div class="card1" style="margin-top: 0.5rem">
        <div class="card1-title">用户信息：</div>
        <div class="card1-text">手机号：{{ phone }}</div>
      </div>
      <div class="title">发放代金券</div>
      <div class="card" style="margin-top: .3rem">
        <div>
          <span class="card-span">代金券：</span>
          <input class="input" type="number" v-model="money" placeholder="请输入金额" />
          <span class="card-span" style="margin-left: 0.1rem">元</span>
        </div>
        <div class="card-span1">用户余额剩余：{{ integral }}元</div>
        <div>
          <span class="card-span">有效期：</span>
          <input class="input" type="number" v-model="days" placeholder="请输入天数" />
          <span class="card-span" style="margin-left: 0.1rem">天</span>
        </div>
        <div class="card-span1">默认365天</div>
      </div>
      <div class="sao" @click="sumbit">确认发放</div>
      <div class="title" style="color: red;margin-top: .5rem;" v-if="role == 2">扣除积分</div>
      <div class="card" style="margin-top: .3rem" v-if="role == 2">
        <div style="position:relative">
          <span class="card-spans">积分：</span>
          <input class="input" style=" margin-left: 1.4rem;" type="number" v-model="moneys" placeholder="请输入扣除积分" />
          <span class="card-span" style="margin-left: 0.1rem">元</span>
        </div>
        <div class="card-span1">用户余额剩余：{{ integral }}元</div>
        <div style="position:relative;">
          <div class="card-spans">描述：</div>
          <textarea class="inputs" v-model="dec" placeholder="请输入原因"></textarea>
        </div>
      </div>
      <div class="sao" @click="sumbits" style="background-color: red;color: #fff;" v-if="role == 2">确认扣除</div>
      <van-dialog v-model="isSureShow" title="温馨提示" :show-cancel-button="false" :showConfirmButton="false" width="8.5rem">
        <div style="padding: 0 0.5rem 0.5rem">
          <div class="tipsContent">
            发放积分为
            <span style="color: red; font-weight: 600">{{ money }}</span>
            元,是否确认发放。
          </div>
          <div style="
              display: flex;
              align-items: center;
              justify-content: space-around;
              padding: 0 0.5rem;
            ">
            <button class="cancelBtn" @click="cancel">
              <van-loading v-if="loadings" size="24px">发放中...</van-loading>
              <span v-else>取消</span>
            </button>
            <button class="confirmBtn" @click="sureFn">
              <van-loading v-if="loadings" size="24px">发放中...</van-loading>
              <span v-else>发放</span>
            </button>
          </div>
        </div>
      </van-dialog>
      <van-dialog v-model="isSureShows" title="温馨提示" :show-cancel-button="false" :showConfirmButton="false"
        width="8.5rem">
        <div style="padding: 0 0.5rem 0.5rem">
          <div class="tipsContent">
            扣除积分为
            <span style="color: red; font-weight: 600">{{ moneys }}</span>
            元,是否确认扣除。
          </div>
          <div style="
              display: flex;
              align-items: center;
              justify-content: space-around;
              padding: 0 0.5rem;
            ">
            <button class="cancelBtn" @click="cancels">
              <van-loading v-if="loading" size="24px">扣除中...</van-loading>
              <span v-else>取消</span>
            </button>
            <button class="confirmBtn" @click="deleteFn">
              <van-loading v-if="loading" size="24px">扣除中...</van-loading>
              <span v-else>扣除</span>
            </button>
          </div>
        </div>
      </van-dialog>
    </div>
  </div>
</template>

<script>
import getUrldata from "@/utils/getUrl.js";
import { addVoucher, scan, deductVoucher } from "@/api/fulfil.js";
export default {
  data() {
    return {
      phone: "",
      value: "",
      integral: "",
      query: {},
      userid: "",
      money: "",
      days: "",
      isSureShow: false,
      isSureShows: false,
      loadings: false,
      loading: false,
      moneys: '',//扣除积分
      dec: '',//备注
      agentid: '',
      role: '',
    };
  },
  created() {
    let role = localStorage.getItem("role");
    this.role = role;
    let agentid = localStorage.getItem("agentid");
    console.log(agentid, 'agentid');
    if (!agentid) {
      this.$Toast("请先登录!");
      this.$router.push({
        path: "/login",
      });
      return false;
    }

    let values = getUrldata("value");
    values = values.replace(/%3D/g, "=");
    console.log(values, "values");
    if (values) {
      this.value = values;
      this.getIndex();
    } else {
      console.log("获取失败！");
    }
  },
  methods: {
    // 获取用户id
    getUrlParam(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
    cancel() {
      this.isSureShow = false;
    },
    cancels() {
      this.isSureShows = false;
    },
    close() {
      this.$router.go(-1);
    },
    /* 获取用户信息 */
    getIndex() {
      scan({
        agentid: localStorage.getItem("agentid"),
        value: this.value,
      })
        .then((res) => {
          this.phone = res.data.data.phone;
          this.integral = res.data.data.voucher;
          this.userid = res.data.data.id;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    /* 赠券 */
    sureFn() {
      this.loadings = true;
      addVoucher({
        store: localStorage.getItem("stores"),
        userid: this.userid,
        money: this.money,
        days: this.days,
      })
        .then((res) => {
          if (res.data.code == 0) {
            this.$Toast("赠券成功！");
            this.money = "";
            this.days = "";
            this.isSureShow = false;
            setTimeout(() => {
              this.$router.push({
                path: "/index",
              });
            }, 2000);
          } else {
            this.$Toast(res.data.msg);
          }
          this.loadings = false;
        })
        .catch((error) => {
          this.loadings = false;
          console.log(error);
        });
    },
    /* 扣除积分 */
    deleteFn() {
      this.loading = true;
      deductVoucher({
        store: localStorage.getItem("stores"),
        userid: this.userid, //'6370ed273d8f44228bbebb0f20de6491',
        money: this.moneys,
        dec: this.dec,
        agentid: localStorage.getItem("agentid"),  //'be37821d38234972a6dd0d8da9940935',
      })
        .then((res) => {
          if (res.data.code == 0) {
            this.$Toast("扣除积分成功！");
            this.moneys = "";
            this.dec = "";
            this.isSureShows = false;
            setTimeout(() => {
              this.$router.push({
                path: "/index",
              });
            }, 2000);
          } else {
            this.$Toast(res.data.msg);
          }
          this.loading = false;
        })
        .catch((error) => {
          this.loading = false;
          console.log(error);
        });
    },
    sumbit() {
      if (!this.money) {
        this.$Toast("赠券金额不能为空！");
      } else {
        this.isSureShow = true;
      }
    },
    //扣除积分
    sumbits() {
      if (!this.moneys) {
        this.$Toast("扣除积分不能为空！");
      } else {
        this.isSureShows = true;
      }
    },
  },
};
</script>

<style scoped>
.content {
  overflow: hidden;
  width: 100%;
  height: 100vh;
  background: #e1f7fc;
}

.card1 {
  overflow: hidden;
  margin: 0.3rem auto 0;
  padding: .25rem 0.5rem;
  width: 84%;
  background: #C7E9F1;
  border-radius: 0.375rem;
}

.card1-title {
  margin-top: 0.2rem;
  font-size: 0.4125rem;
  font-weight: 600;
  color: #000;
}

.card1-text {
  margin: 0.25rem 0 0 0.2rem;
  font-size: 0.3125rem;
  font-weight: 600;
  color: #000;
}

.card1-text1 {
  margin: 0.25rem 0 0 0.2rem;
  font-size: 0.3125rem;
  font-weight: 600;
  color: #000;
  text-align: center;
}

.btn {
  margin-top: 0.6rem;
}

.title {
  margin: .3rem 0 0 0;
  padding: 0 .3rem;
  font-size: .4rem;
  color: #ffbe4c;
  font-weight: 600;
}

.btn1 {
  float: left;
  margin-left: 2rem;
  display: inline-block;
  width: 2.4rem;
  height: 0.75rem;
  color: #000;
  text-align: center;
  line-height: 0.75rem;
  border-radius: 0.375rem;
  background-color: #3cc9e8;
}

.btn2 {
  margin-right: 2rem;
  float: right;
  display: inline-block;
  width: 2.5rem;
  height: 0.75rem;
  line-height: 0.75rem;
  text-align: center;
  color: #000;
  border-radius: 0.375rem;
  background-color: #ffbe4c;
}

.sao {
  margin: .4rem auto 0;
  width: 60%;
  text-align: center;
  height: 1rem;
  line-height: 1rem;
  font-size: 0.35rem;
  border-radius: 0.5rem;
  background-color: #ffbe4c;
  font-weight: 600;
}

.card {
  padding: .5rem;
  overflow: hidden;
  margin: 0.3rem auto 0;
  width: 84%;
  border-radius: 0.375rem;
  background-color: #C7E9F1;
}

.input {
  margin-left: 0.1rem;
  margin-top: 0.2875rem;
  width: 60%;
  background: white;
  border: none;
  padding: 0.1875rem 0.25rem;
  border-radius: 0.25rem;
  border-inline: 2px solid transparent;
}

.inputs {
  margin-left: 1.4rem;
  margin-top: 0.2875rem;
  width: 60%;
  background: white;
  border: none;
  padding: 0.1875rem 0.25rem;
  border-radius: 0.25rem;
  border-inline: 2px solid transparent;
}

.card-span {
  font-size: 0.32rem;
  font-weight: 600;
}

.card-spans {
  position: absolute;
  top: .4rem;
  left: 0;
  font-size: 0.32rem;
  font-weight: 600;

}

.card-span1 {
  margin: 0.2rem 0 0 2.6rem;
  font-size: 0.32rem;
  font-weight: 600;
}

.img1 {
  display: inline-block;
  width: 27px;
  height: 48px;
  vertical-align: middle;
}

.return {
  margin: 20px 0 0 30px;
  font-size: 40px;
}

.return-p {
  margin-left: 20px;
  display: inline-block;
  vertical-align: middle;
}

.confirmBtn {
  margin-top: 0.5rem;
  width: 3rem;
  height: 1rem;
  background: linear-gradient(315deg, #4441ee 0%, #920ccc 100%);
  box-shadow: 0rpx 4rpx 16rpx 0rpx rgba(0, 0, 0, 0.25);
  border-radius: 0.6rem;
  opacity: 1;
  color: #ffffff;
  border: none;
  font-size: 0.4rem;
}

.cancelBtn {
  margin-top: 0.5rem;
  width: 3rem;
  height: 1rem;
  background: #e2c7ff;
  box-shadow: 0rpx 4rpx 16rpx 0rpx rgba(0, 0, 0, 0.25);
  border-radius: 0.6rem;
  opacity: 1;
  color: #6533d2;
  border: none;
  font-size: 0.4rem;
}

.van-dialog {}

.tipsContent {
  margin: 0.5rem 0 1rem;
  display: inline-block;
  font-size: 0.4rem;
  font-family: PingFang SC-Medium, PingFang SC;
  font-weight: 500;
  color: #111111;
}
</style>